package cn.iocoder.yudao.module.wine.service.memberdeadline;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.wine.api.dto.MemberDeadlineDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;

import cn.iocoder.yudao.module.wine.controller.admin.memberdeadline.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.memberdeadline.MemberDeadlineDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.memberdeadline.MemberDeadlineMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 会员期限 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class MemberDeadlineServiceImpl implements MemberDeadlineService {

    @Resource
    private MemberDeadlineMapper memberDeadlineMapper;

    @Override
    public Long createMemberDeadline(MemberDeadlineSaveReqVO createReqVO) {
        // 插入
        MemberDeadlineDO memberDeadline = BeanUtils.toBean(createReqVO, MemberDeadlineDO.class);
        memberDeadlineMapper.insert(memberDeadline);
        // 返回
        return memberDeadline.getId();
    }

    @Override
    public void updateMemberDeadline(MemberDeadlineSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberDeadlineExists(updateReqVO.getId());
        // 更新
        MemberDeadlineDO updateObj = BeanUtils.toBean(updateReqVO, MemberDeadlineDO.class);
        memberDeadlineMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberDeadline(Long id) {
        // 校验存在
        validateMemberDeadlineExists(id);
        // 删除
        memberDeadlineMapper.deleteById(id);
    }

    private void validateMemberDeadlineExists(Long id) {
        if (memberDeadlineMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public MemberDeadlineDO getMemberDeadline(Long id) {
        return memberDeadlineMapper.selectById(id);
    }

    @Override
    public PageResult<MemberDeadlineRespVO> getMemberDeadlinePage(MemberDeadlinePageReqVO pageReqVO) {
        return memberDeadlineMapper.selectPage(pageReqVO);
    }

    /**
     * 获取用户期限(有效期内的数据)
     * @param userId
     * @return
     */
    @Override
    public MemberDeadlineDO getMemberDeadlineInfoByUserId(Long userId) {
        LocalDate now = LocalDate.now();
        return memberDeadlineMapper.selectOne(new LambdaQueryWrapperX<MemberDeadlineDO>()
                .eq(MemberDeadlineDO::getUserId,userId)
                .ge(MemberDeadlineDO::getEndDate,now)
                .le(MemberDeadlineDO::getStartDate,now));

    }
    /**
     * 获取用户期限
     * @param userId
     * @return
     */
    @Override
    public MemberDeadlineDO getMemberDeadlineByUserId(Long userId) {
        return memberDeadlineMapper.selectOne(new LambdaQueryWrapperX<MemberDeadlineDO>()
                .eq(MemberDeadlineDO::getUserId,userId)
        );
    }

    @Override
    public Boolean addMemberDeadline(MemberDeadlineDTO memberDeadlineDTO) {
        MemberDeadlineDO bean = BeanUtils.toBean(memberDeadlineDTO, MemberDeadlineDO.class);
        return memberDeadlineMapper.insert(bean) > 0;
    }

    @Override
    public Boolean editMemberDeadline(MemberDeadlineDTO memberDeadlineDTO) {
        MemberDeadlineDO bean = BeanUtils.toBean(memberDeadlineDTO, MemberDeadlineDO.class);
        return memberDeadlineMapper.updateById(bean) > 0;

    }

    @Override
    public MemberDeadlineDO appGetMemberDeadline(Long id) {
        return memberDeadlineMapper.selectOne(new LambdaQueryWrapperX<MemberDeadlineDO>()
                .eqIfPresent(MemberDeadlineDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .eq(MemberDeadlineDO::getId,id));

    }


}
